<template>
  <div class="devices-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>设备列表</span>
          <el-button type="primary" @click="handleAdd">新增设备</el-button>
        </div>
      </template>

      <el-table :data="deviceList" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="设备名称" />
        <el-table-column prop="type" label="设备类型" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === '在线' ? 'success' : 'info'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="IP地址" width="150" />
        <el-table-column prop="location" label="位置" width="150" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination v-model:current-page="pagination.page" v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50]" :total="pagination.total" layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

export default {
  name: 'Devices',
  setup() {
    const router = useRouter()
    const loading = ref(false)
    const deviceList = ref([])

    const pagination = reactive({
      page: 1,
      size: 10,
      total: 0
    })

    // 模拟设备数据
    const mockDeviceList = [
      {
        id: 1,
        name: '摄像头001',
        type: '摄像头',
        status: '在线',
        ip: '192.168.1.101',
        location: '大厅',
        createTime: '2023-01-15 09:30:00'
      },
      {
        id: 2,
        name: '传感器002',
        type: '温湿度传感器',
        status: '离线',
        ip: '192.168.1.102',
        location: '仓库',
        createTime: '2023-01-16 14:20:00'
      },
      {
        id: 3,
        name: '门禁设备003',
        type: '门禁',
        status: '在线',
        ip: '192.168.1.103',
        location: '办公室',
        createTime: '2023-01-17 11:45:00'
      }
    ]

    // 获取设备列表
    const fetchDeviceList = async () => {
      try {
        loading.value = true
        // 模拟API调用延迟
        await new Promise(resolve => setTimeout(resolve, 500))
        deviceList.value = mockDeviceList
        pagination.total = mockDeviceList.length
      } catch (error) {
        ElMessage.error('获取设备列表失败: ' + error.message)
      } finally {
        loading.value = false
      }
    }

    // 处理分页大小变化
    const handleSizeChange = (val) => {
      pagination.size = val
      fetchDeviceList()
    }

    // 处理当前页变化
    const handleCurrentChange = (val) => {
      pagination.page = val
      fetchDeviceList()
    }

    // 处理新增设备
    const handleAdd = () => {
      router.push('/device/add')
    }

    // 处理查看设备
    const handleView = (row) => {
      router.push(`/device/detail?id=${row.id}`)
    }

    // 处理编辑设备
    const handleEdit = (row) => {
      router.push(`/device/edit?id=${row.id}`)
    }

    // 处理删除设备
    const handleDelete = (row) => {
      ElMessageBox.confirm(`确定要删除设备 ${row.name} 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 模拟删除操作
        ElMessage.success('删除成功')
        fetchDeviceList()
      }).catch(() => {
        // 取消删除
      })
    }

    // 组件挂载时获取数据
    onMounted(() => {
      fetchDeviceList()
    })

    return {
      loading,
      deviceList,
      pagination,
      fetchDeviceList,
      handleSizeChange,
      handleCurrentChange,
      handleAdd,
      handleView,
      handleEdit,
      handleDelete
    }
  }
}
</script>

<style scoped>
.devices-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>